package com.itshen.router;

import com.itshen.handler.UserHandler;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.web.reactive.function.server.RequestPredicates;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.RouterFunctions;
import org.springframework.web.reactive.function.server.ServerResponse;

/**
 * @author: stephen.shen
 * @create: 2018-12-07 10:28
 */
@Configuration
public class AllRouter {

    @Bean
    RouterFunction<ServerResponse> userRouter(UserHandler handler) {
        return RouterFunctions.nest(
                RequestPredicates.path("/user"),
                RouterFunctions.route(RequestPredicates.GET(""), handler::getAll)
                        .andRoute(RequestPredicates.POST("").and(RequestPredicates.accept(MediaType.APPLICATION_JSON_UTF8)), handler::create)
                        .andRoute(RequestPredicates.DELETE("/{id}"), handler::deleteUserById));
    }
}
